<?php
/**
 * 
 * Controller SetupInbaund
 * @author Natanael
 * 
 */
class SetupInbaund extends Controller
{
    public function __construct()
	{
	    parent::Controller();
	    $this->load->library(array('chcalendario','ChFopen','email'));
	    $this->load->model(array('mscript', 'usuarios', 'mfrom','msetupmessages', 'mservicios'));
	}
	
	public function index()
	{
        $this->load->view('main', array('content_page' => 'setupinbaund_list','menutop' => 0));	
	}
	
    public function data()
	{
	    $page = $this->input->post('page') ? $this->input->post('page') : 1; // Pagina Actual o 1 por default
		$sortname = $this->input->post('sidx') ? $this->input->post('sidx') : 'Id_Service'; // Columna para el order by
		$sortorder = $this->input->post('sord') ? $this->input->post('sord') : 'desc'; // Orden (asc|desc)
		$limit = $this->input->post('rows') ? $this->input->post('rows') : 30;
		$start = $limit * $page - $limit;
		if($start < 0) $start = 0;
  
		      header("Expires: Mon, 26 Jul 1997 05:00:00 GMT" );
		      header("Last-Modified: " . gmdate( "D, d M Y H:i:s" ) . "GMT" );
		      header("Cache-Control: no-cache, must-revalidate" );
		      header("Pragma: no-cache" );

        $records = $this->msetupmessages->loadObjeto($start, $limit, $sortname, $sortorder);

		$totalRecords = count($records);

		$totalPages = ceil($totalRecords/$limit);
		  if ($page > $totalPages) $page = $totalPages;

		$json_output = array(
		         'page'		=> $page,
		         'total'	=> $totalPages,
		         'records'	=> $totalRecords,
		         'rows'		=> array());

		if($records != false){
			 foreach($records as $objetos){
			 	     $script = $this->mscript->LoadRegistroById($objetos->getIdScript());
				     $servicio = $this->mservicios->LoadServicioById($objetos->getIdService());
			 	     
			 	     $json_output['rows'][] = array(
				          'Id'           => $objetos->getId(),
				          'Id_Service'   => $servicio->getNombreServicio(),
				          'Id_Script'    => $script->getTitleScript(),
				          'Type'         => $objetos->getType());
			 }
	    }
		 echo json_encode($json_output);
    }
    
   public function add()
   {
   	   $objetoServicio = $this->mservicios->Loadservicios();
   	   $objetoScript = $this->mscript->loadScript();
       $this->load->view('main', array('content_page'    => 'setupinbaund_add',
                                       'menutop'         => 0,
                                       'objetoServicio'  => $objetoServicio,
                                       'objetoScript'    => $objetoScript));	
   }
   
   public function edit($id = false)
   {  
   	   if ($id === false){
   	   	 $this->session->set_flashdata('error_title', 'Ocurrio un error');
         $this->session->set_flashdata('error', 1);
   	     redirect('setupinbaund'); 
   	   }
   	   
   	   $objetoServicio = $this->mservicios->Loadservicios();
   	   $objetoScript = $this->mscript->loadScript();
   	   $objetoDataSetup = $this->msetupmessages->LoadObjetoById($id);
   	   
       $this->load->view('main', array('content_page'    => 'setupinbaund_edit',
                                       'menutop'         => 0,
                                       'objetoServicio'  => $objetoServicio,
                                       'objetoScript'    => $objetoScript,
                                       'objetoDataSetup' => $objetoDataSetup));
   }
   
   public function save()
   {
       $objetoData = array("Id_Service"    => $this->input->post('service'),
                           "Id_Script"     => $this->input->post('script'),
                           "Type"          => $this->input->post('tipo'));
       
         if ($this->input->post('id') == false) {
         	  if ($this->msetupmessages->addReguistro($objetoData) == true){
         	        $this->session->set_flashdata('error_title', 'Se creo un registro');
         	        $this->session->set_flashdata('error', 2);
         	  }else{
         	        $this->session->set_flashdata('error_title', 'Ocurrio un error');
         	        $this->session->set_flashdata('error', 1);
         	  }
         }else{
              if ($this->msetupmessages->editReguistro($objetoData, $this->input->post('id')) == true){
         	        $this->session->set_flashdata('error_title', 'Se modifico un registro');
         	        $this->session->set_flashdata('error', 2);
         	  }else{
         	        $this->session->set_flashdata('error_title', 'Ocurrio un error');
         	        $this->session->set_flashdata('error', 1);
         	  }         
         }
         
        redirect('setupinbaund'); 
   }
   
   public function delete()
   {
       $id = $this->input->post('delete_rows');
      
       if ($id != false){
          $this->msetupmessages->deleteReguistro($id);	
       }
      
   }
}